package com.swgk.core.util;

import android.content.Context;
import android.content.SharedPreferences;

import org.apache.commons.lang3.StringUtils;

/**
 * SP工具类
 */
public class SharedPreferenceUtil {
    public static final String resourcePCToken = "resourcePCToken";
    public static final String resourceAPPToken = "resourceAPPToken";
    public static final String PREFERENCE_NAME = "saveInfo";
    public static final String PREFERENCE_COMMON = "common";
    //当前城市id
    public static final String PROVINCE_ID = "provinceId";
    //当前城市的经度
    public static final String LONGITUDE_ID = "longitudeId";//切换城市后的省会定位
    public static final String LONGITUDE = "longitude";//当前实际定位
    //当前城市的维度
    public static final String LATITUDE_ID = "latitudeId";//切换城市后的省会定位
    public static final String LATITUDE = "latitude";//当前实际定位
    public static final String QG_TOKEN = "qiGuanToken";//企管帮传的新token

    //默认乌鲁木齐经纬度
    public static final Double DEFAULT_LATITUDE = 43.82539;
    public static final Double DEFAULT_LONGITUDE = 87.616842;

    //登录token
    public static final String LOGIN_TOKEN = "loginToken";
    //pc token
    public static final String LOGIN_PC_TOKEN = "LOGIN_PC_TOKEN";
    //登录电话
    public static final String LOGIN_PHONE = "loginPhone";
    //登录人角色
    public static final String LOGIN_ROLE = "loginRole";
    //登录人姓名
    public static final String REAL_NAME = "realName";
    //登录人ID
    public static final String REAL_ID = "realId";
    //登录人身份证号
    public static final String ID_CARD = "idCard";
    //店铺id
    public static final String SHOP_ID = "shopId";
    //个人中心显示名称
    public static final String USER_NAME = "userName";
    //个人中心用户no
    public static final String USER_NO = "userNo";
    //个人中心公司名称或者团队名称
    public static final String COMPANY_NAME = "companyName";
    //个人中心公司no或者团队no
    public static final String COMPANY_NO = "companyNo";

    public static final String PC_TOKEN = "PC_TOKEN";
    //类型 0个人 1企业 2团队 3组织
    public static final String COM_TYPE = "com_type";
    //个人中心公司名称
    public static final String COMP_LOGO = "compLogo";
    //是否个人实名认证(boolean)
    public static final String PERSON_AUTH = "personAuth";
    //企业no
    public static final String ENTERPRISE_NO = "enterpriseNo";

    //是否司机认证
    public static final String DRIVER_AUTH = "driverAuth";

    //认证专用
    public static final String ISFRIIST = "isone";

    //是否有认证或加入企业
    public static final String IS_AUTH_OR_JOIN_COMPANY = "isAuthOrJoinCompany";

    //企业认证状态四种
    /**
     *  NONE：      未认证
     *  STANDBY：   认证中
     *  FAIL：     认证失败
     *  SUCCESS：  认证成功
     */
    @Deprecated
    public static final String ENTERPRISE_AUTH = "enterpriseAuth";

    //工队发布 保存的数据
    public static final String PACKERS_PUBLISH_DATA = "packers_publish_data";
    //设备发布 保存的数据
    public static final String DEVICE_PUBLISH_DATA = "device_publish_data";
    //机械发布 保存的数据
    public static final String MACHINE_PUBLISH_DATA = "machine_publish_data";
    //分享自定义 频道 保存的数据
    public static final String SHARE_CHANNEL_DATA = "share_channel_data";
    //货物类型
    public static final String GOODS_INFO_TYPE = "goodsInfoType";
    //货车类型
    public static final String CARS_INFO_TYPE = "carsInfoType";
    //货车长度
    public static final String CARS_INFO_LENGTH = "carsInfoLength";
    //运输方式
    public static final String TRANSPORT_TYPE = "transportType";
    //装卸方式
    public static final String CARS_HANDLING_TYPE = "carsHandlingType";
    //付款方式
    public static final String PAY_TYPE = "payType";
    //分享侧滑用户id
    public static final String SHARE_PALY_USERNO = "sharePalyUserno";
    //发票方式
    public static final String INVOICE_TYPE = "invoiceType";
    public static final String DEP_NAME = "DEP_NAME";
    // 司机认证的车牌号
    public static final String CAR_PUBLISH_NO_DRIVER_AUTHENTICATION = "CAR_PUBLISH_NO_DRIVER_AUTHENTICATION";
    // 切換地址
    public static final String URL = "url_set";
    //集采-搜索——历史搜索词汇
    public static final String JICAI_SEARCH_HISTORY = "JiCaiSearchHistoryWord";    // 分享推荐下标保存
    //租赁-搜索-历史搜索词汇
    public static final String ZULIN_SEARCH_HISTORY="ZuLinSearchHistoryWord";

    //租赁-搜索-地图-地址-历史搜索词汇
    public static final String ZULIN_MAP_ADDR_SEARCH_HISTORY="ZuLinMapAddrSearchHistoryWord";
    public static final String SHARE_HOME_POSITION = "ShareHomePosition";
    //手动刷新
    public static final String MANUAL_REFRESH = "manual_refresh";
    // 历史搜索
    public static final String HISTORY_SEARCH = "historySearch";
    // relevance 是否关联企业 0:未关联1:已关联
    public static final String RELEVANCE = "relevance";
    // 存储所有权限json数据,用于判断个人中心数据
    public static final String IS_DATA = "isData";
    public static final String PERMISSION_HUI_YUAN = "permission_hui_yuan";//保存会员权限数据
    // 存储所有子菜单权限json数据,用于判断子菜单权限
    public static final String CHILD_MENU = "childMenu";
    // 存储招采大搜索的本地历史纪录
    public static final String PICK_SEARCH = "pickSearch";
    // 商机本地历史搜索数据
    public static final String HISTORY_SEARCH_BUSINESS = "business_history_search";

    //定位的城市
    public static final String City="city";
    public static final String Province="province";
    public static final String adCode="adCode";
    public static final String Area="area";
    public static final String AoiName="AoiName";
    public static final String Address="address";

    //切换省份的保存信息
    public static final String AppProvinceCode="appprovince";

    public static final String FLAG_MESSAGE_NOTICE = "flag_message_notice"; //收到消息是否提示
    public static final String FLAG_PUSH_NOTICE = "flag_push_notice";       //推送通知是否提示
    public static final String APP_IS_FOREGROUND = "app_is_foreground";     //应用是否在前台
    public static final String IS_LIMIT_MULTI_LOGIN = "isLimitMultiLogin";     //是否被挤掉了

    public static final String GROUP_NUM = "GROUP_NUM";//群人数（用于显示当前群内消息当前的未读数计算）

    private static SharedPreferences mSharedPreferences;
    private static SharedPreferences commonSharedPreferences;
    private static SharedPreferenceUtil mSharedpreferenceUtil;
    private static SharedPreferences.Editor editor;

    public static final String AREA_CODE="areaCode"; //当前选中的城市Code
    public static final String AREA_NAME="areaName"; //当前选中的地区名称
    //当前切换后，选中城市的经纬度
    public static final String SelectLONGITUDE = "Select_longitude";
    public static final String SelectLATITUDE = "Select_latitude";

    //大首页刷新位置专用城市经纬度
    public static final String HOME_LONGITUDE = "home_longitude";
    public static final String HOME_LATITUDE = "home_latitude";

    private SharedPreferenceUtil(Context cxt) {
        mSharedPreferences = cxt.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        editor = mSharedPreferences.edit();
    }

    public static synchronized void init(Context cxt) {
        if (mSharedpreferenceUtil == null) {
            mSharedpreferenceUtil = new SharedPreferenceUtil(cxt);
        }
    }

    /**
     * 单例模式，获取instance实例
     *
     * @return
     */
    public synchronized static SharedPreferenceUtil getInstance() {
        if (mSharedpreferenceUtil == null) {
            throw new RuntimeException("please init first!");
        }

        return mSharedpreferenceUtil;
    }

    final public void saveData(String key, String value) {
        editor.putString(key, value).commit();
    }

    final public void saveData(String key, int value) {
        editor.putInt(key, value).commit();
    }

    final public void saveData(String key, boolean value) {
        editor.putBoolean(key, value).commit();
    }

    final public void saveData(String key, float value) {
        editor.putFloat(key, value).commit();
    }

    final public void saveData(String key, long value) {
        editor.putLong(key, value).commit();
    }

    //
    final public void saveString(String key, String value) {
        editor.putString(key, value).commit();
    }

    final public String getString(String key, String defaultValue) {
        return mSharedPreferences.getString(key, defaultValue);
    }

    final public void saveBoolean(String key, boolean value) {
        editor.putBoolean(key, value).commit();
    }

    final public boolean getBoolean(String key, boolean defaultValue) {
        return mSharedPreferences.getBoolean(key, defaultValue);
    }

    final public String getSaveStringData(String key, String defaultValue) {
        return mSharedPreferences.getString(key, defaultValue);
    }

    final public int getSaveIntData(String key, int defaultValue) {
        return mSharedPreferences.getInt(key, defaultValue);
    }

    final public boolean getSaveBooleanData(String key, boolean defaultValue) {
        return mSharedPreferences.getBoolean(key, defaultValue);
    }

    final public float getSaveFloatData(String key, float defaultValue) {
        return mSharedPreferences.getFloat(key, defaultValue);
    }

    final public long getSaveLongData(String key, long defaultValue) {
        return mSharedPreferences.getLong(key, defaultValue);
    }

    final public void cleatData() {
        editor.clear().commit();
    }

    //数组转字符串
    final public String arryToString(String[] arry) {
        String str = StringUtils.join(arry, ","); // 数组转字符串(逗号分隔)(推荐)
        return str;
    }

    //字符串转数组
    final public String[] stringToArry(String str) {
        String[] arr = str.split(","); // 用,分割
        return arr;
    }
}
